//单链表实现队列
public class MyQueue<E> {
     class Node{
        private E val;
        private Node next;

        public Node(E val) {
            this.val = val;
        }
    }
    //头节点
    private Node head;
    //尾节点
    private Node last;

    //显示
    public void display(){
        Node cur=head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    //入队列
    public void offer(E e) {
        Node node=new Node(e);
        if(head==null){
            head=node;
            last=node;
        }else {
            last.next=node;
            last=node;
        }
    }
    //出队列
    public E poll(){
        if(head==null){
            throw new RuntimeException();
        }
        E tmp=head.val;
        if(head.next==null){
            last=null;
        }
        head=head.next;
        return tmp;
    }
    //获取队头元素
    public E peek(){
        return head.val;
    }
    //获取队列中有效元素个数
    public int size(){
        Node cur=head;
        int count=0;
        while (cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }
    //检测队列是否为空
    public boolean isEmpty(){
        if(size()==0)return true;
        return false;
    }
}
